package com.bancomer.main;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Properties;

import org.apache.log4j.Logger;

import com.bancomer.batch.BatchController;
import com.bancomer.batch.Constants;
import com.bancomer.batch.impl.BatchContext;
import com.bancomer.batch.pojo.Alert;
import com.bancomer.batch.pojo.FileHelper;
import com.bancomer.batch.pojo.PigScript;
import com.bancomer.batch.util.LinuxCommand;
import com.bancomer.batch.validators.impl.LayoutValidator;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.StaxDriver;


public class MainBatchController {

	public static void main(String[] args) throws Exception {
		Logger logger = Logger.getLogger("MainBatchController");
		XStream xstream = new XStream(new StaxDriver());
		
		String pathFileAlert = args[0];
		//to do reflection
		String impl="com.bancomer.batch.impl.BatchCotrollerImpl";
		
		xstream.alias("Alert", Alert.class);
		xstream.alias("FileHelper", FileHelper.class);
		xstream.alias("PigScript", PigScript.class);
		Alert alert= (Alert) xstream.fromXML(new File(pathFileAlert));
		
		@SuppressWarnings("unchecked")
		Class<BatchController> userClass = (Class<BatchController>) Class.forName(impl);      
		
		BatchController controller= (BatchController) userClass.newInstance();
		
		BatchContext batchContext= new BatchContext();
		
		batchContext.setContextParam(new HashMap<String,Object>());			
		
		controller.setBatchContext(batchContext);
		
		try{ 
			batchContext.getContextParam().put("alert", alert);
			System.out.println();
			
			controller.init();
			logger.info("Iniciando el proceso de "+alert.getName());
			logger.info("ID del proceso "+new Date().getTime());
			if(batchContext.getContinueFlow()==false)
				throw new Exception(batchContext.getMsgError());
			logger.info("init: SUCCES ");
			
			controller.validateFiles();
			if(batchContext.getContinueFlow()==false)
				throw new Exception(batchContext.getMsgError());
			logger.info("validateFiles: SUCCES ");
			
			controller.putFilesToHdfs();
			if(batchContext.getContinueFlow()==false)
				throw new Exception(batchContext.getMsgError());
			logger.info("putFilesToHdfs: SUCCES ");
			
			controller.executePig();
			if(batchContext.getContinueFlow()==false)
				throw new Exception(batchContext.getMsgError());
			logger.info("executePig: SUCCES ");
			
			controller.getFilesOnHdfs();
			if(batchContext.getContinueFlow()==false)
				throw new Exception(batchContext.getMsgError());
			logger.info("getFilesOnHdfs: SUCCES ");
			
			controller.prepareResponseFiles();
			if(batchContext.getContinueFlow()==false)
				throw new Exception(batchContext.getMsgError());
			logger.info("prepareResponseFiles: SUCCES ");
			
			controller.sendResponseFiles();
			if(batchContext.getContinueFlow()==false)
				throw new Exception(batchContext.getMsgError());
			logger.info("sendResponseFiles: SUCCES ");
		
		}catch(Exception e){
			logger.error("Error: "+e.getMessage());
			for(int i = 0; i<e.getStackTrace().length;i++){
				logger.error(e.getStackTrace()[i]);
			}
			throw e;
		}
	}

}
